package com.mryt.reimburse;

import com.alibaba.fastjson.JSON;
import com.google.common.util.concurrent.AtomicDouble;
import com.mryt.bean.Dache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @Auther: penghaozhong
 * @Date: 2018/12/27 14:40
 */
@Component
@Slf4j
public class RemiburseService {


    @Value("${mryt.perMonth.subsidy.meal}")
    private int mealSubsidy;

    private List<Dache> dacheList;


    public RemiburseService() {
        loadData();
    }

    private void loadData(){
        String json = null;
        try {
            json = IOUtils.toString(this.getClass().getResourceAsStream("/dache.json"), StandardCharsets.UTF_8.name());
        } catch (IOException e) {
            e.printStackTrace();
        }

        dacheList = JSON.parseArray(json, Dache.class);
    }

    public double   calPrice(){
        AtomicDouble total = new AtomicDouble();
        dacheList.forEach(d->{

            LocalDate localDate = d.getEdate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            DayOfWeek dayOfWeek = localDate.getDayOfWeek();
           // 周六日加班 餐补*2
            if(dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY){
                total.addAndGet(mealSubsidy * 2);
           }else{
                total.addAndGet(mealSubsidy);
            }

            log.info("{} {}  打车 {} 累计:{}",localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")),dayOfWeek,d.getActlamount(),total.get());
        });

        return total.doubleValue();
    }


    public int getMealSubsidy() {
        return mealSubsidy;
    }

    public void setMealSubsidy(int mealSubsidy) {
        this.mealSubsidy = mealSubsidy;
    }
}
